package com.java.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;

public class DocumentEventTest extends JFrame{
	private JButton save;
	private JTextArea area;
	public DocumentEventTest(){
		save=new JButton("保存");
		area=new JTextArea(10,20);
		add(save,BorderLayout.EAST);
		add(area,BorderLayout.CENTER);
	}
	public void addEventHandler(){
		save.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				save.setEnabled(false);
			}
			
		});
		Document doc=area.getDocument();
		doc.addDocumentListener(new DocumentListener(){

			public void changedUpdate(DocumentEvent e) {
				save.setEnabled(true);
			}

			public void insertUpdate(DocumentEvent e) {
				save.setEnabled(true);
			}

			public void removeUpdate(DocumentEvent e) {
				save.setEnabled(true);
			}
			
		});
		
	}
	public void showMe(){
		addEventHandler();
		this.pack();
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public static void main(String[] args){
		new DocumentEventTest().showMe();
	}
}
